
/* 数组切割 */
export function chunkArray(array, chunkSize = 4) {
  const result = [];
  for (let i = 0; i < array.length; i += chunkSize) {
    result.push(array.slice(i, i + chunkSize));
  }
  return result;
}
 
export function flattenMenuTree(data) {
  // 1. 对每个一级菜单进行深度优先遍历，生成节点序列
  const dfsLists = data.map(category => {
    const list = [];
    function dfs(node) {
      list.push(node);
      if (node.children && node.children.length) {
        for (const child of node.children) {
          dfs(child);
        }
      }
    }
    dfs(category);
    return list;
  });

  // 2. 计算最大列数
  const maxCols = Math.max(...dfsLists.map(list => list.length));

  // 3. 构建结果数组（列优先）
  const result = [];
  for (let col = 0; col < maxCols; col++) {
    for (let row = 0; row < data.length; row++) {
      const node = dfsLists[row][col] || null;
      if (node) {
        // 添加层级信息
        const level = col === 0 ? 0 : (col === 1 ? 1 : 2);
        result.push({
          ...node,
          level: level
        });
      }
    }
  }

  return result;
}

// 使用示例